import axios from './axios'
import qs from 'qs'
import type { PostProps } from '../store'
import { useMainStore } from '@/stores/main'
import router from '@/router'
import { mockConfig } from './configs'

interface ResponeType {
  code: number
  msg: string
  data?: any
}
// 模式，判断模式是不是mock
const mode = import.meta.env.VITE_MOCK

function packResponse(p: Promise<ResponeType>) {
  const store = useMainStore()
  return new Promise((resolve, reject) =>
    // 在code !== 0的情况下，拒绝promise
    p
      .then((res) => {
        console.log('packResponse', res)
        res.data.code === 0 ? resolve(res.data.data) : reject(res)
      })
      .catch((err) => {
        // token过期
        if (err.code === 10005) {
          store.logout()
          router.push('/')
        } else {
          reject(err)
        }
      })
  )
}
// 序列化参数
function requestPromisify(method, url, req?): Promise<any> {
  switch (method) {
    case 'post':
      return packResponse(axios.post(url, req))
    case 'get':
      return packResponse(axios.get(url + '?' + qs.stringify(req)))
    case 'patch':
      return packResponse(axios.patch(url, req))
    case 'delete':
      return packResponse(axios.delete(url + '?' + qs.stringify(req)))
    default:
      return Promise.resolve()
  }
}
// user接口
// 登录
export function login(req) {
  return requestPromisify('post', '/user/login', req)
}
// 注册
export function regist(req) {
  return requestPromisify('post', '/users', req)
}
// 获取当前用户信息
export function getCurrentUser() {
  return requestPromisify('get', '/user/current')
}
// 更新当前用户信息
export function updateCurrentUser(userid, req) {
  return requestPromisify('patch', `/user/${userid}`, req)
}

// column接口
// 获取专栏列表
export function getColumnsList(req) {
  const ans = requestPromisify('get', '/columns', req)
  ans.then((res) => {
    console.log('getColumnsList', res)
  })
  return ans
}
// 获得单个专栏详情
export function getColumnDetail(columnid) {
  columnid = mode === 'mock' ? mockConfig.columnid : columnid
  return requestPromisify('get', `/columns/${columnid}`)
}
// 更新单个专栏信息
export function updateColumn(columnid, req) {
  columnid = mode === 'mock' ? mockConfig.columnid : columnid
  return requestPromisify('patch', `/columns/${columnid}`, req)
}

// post接口
// 新建一篇文章
export function createPost(req: PostProps) {
  return requestPromisify('post', '/posts', req)
}
// 获得单个文章信息
export function getPostDetail(postid) {
  // postid = mode === 'mock' ? mockConfig.columnid : postid
  return requestPromisify('get', `/posts/${postid}`)
}
// 更新单个文章信息
export function updatePost(postid) {
  postid = mode === 'mock' ? mockConfig.columnid : postid
  return requestPromisify('patch', `/posts/${postid}`)
}
// 删除单个文章信息
export function deletePost(postid) {
  postid = mode === 'mock' ? mockConfig.columnid : postid
  return requestPromisify('delete', `/posts/${postid}`)
}
// 获取特定专栏下的文章列表
export function getPostsList({ columnid, page = 1, size = 5 }) {
  columnid = mode === 'mock' ? mockConfig.columnid : columnid
  return requestPromisify('get', `/columns/${columnid}/posts`, { page, size })
}
// file接口

// tip接口
export default {
  addTip: function (req) {
    return axios.post('/tip', req)
  },
}
